package com.bdqn.gulimall.auth.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.bdqn.common.utils.HttpUtils;
import com.bdqn.common.utils.R;
import com.bdqn.common.vo.SocialUser;
import com.bdqn.gulimall.auth.feign.MemberFeignService;
import com.bdqn.common.vo.MemberRespVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 处理社交登录授权请求
 */
@Slf4j
@Controller
public class OAuth2Controller {

    @Resource
    private MemberFeignService memberFeignService;


    @GetMapping("/oauth2.0/gitee/success")
    public String gitee(@RequestParam("code") String code, HttpSession session) throws Exception {
        Map<String,String> map = new HashMap<>();
        System.out.println("/oauth2.0/gitee/success==>"+code);


        map.put("grant_type","authorization_code");
        map.put("redirect_uri","http://auth.yunmall.com/oauth2.0/gitee/success");
        map.put("client_id","1eff7beeb9f1c6905ca7b86a2f48602d9b502cad6c48ecefcd2dc89b1a6fa226");
        map.put("client_secret","334d7cf4d4cc27157c0996b16f625ac2c1549bc490da996b130c7847aba455f2");
        map.put("code",code);


        HttpResponse post = HttpUtils.doPost("https://gitee.com", "/oauth/token", "post",  new HashMap<>(), map, new HashMap<>() );
        if(post.getStatusLine().getStatusCode()==200){
            //取到token

            String json = EntityUtils.toString(post.getEntity());
            System.out.println("EntityUtils.toString==>"+json);
            SocialUser socialUser = JSON.parseObject(json, SocialUser.class);
            System.out.println(socialUser.toString());

            //获取用户信息取出Uid
            Map<String,String> getUserMap = new HashMap<>();
            getUserMap.put("access_token",socialUser.getAccess_token());
            HttpResponse get = HttpUtils.doGet("https://gitee.com", "/api/v5/user", "get", new HashMap<>(), getUserMap);
            JSONObject jsonObject = JSON.parseObject(EntityUtils.toString(get.getEntity()));
            //设置Id
            socialUser.setUId(jsonObject.getString("id"));

            //知道当前登录的是哪个社交用户，如果不是社交用户，就进行自动注册账号
            R oauthlogin = memberFeignService.oauthlogin(socialUser);
            if(oauthlogin.getCode()==0){
                MemberRespVo data = oauthlogin.getData("data", new TypeReference<MemberRespVo>() {
                });
                log.info("登录成功!{}",data.toString());
                session.setAttribute("loginUser",data);
                //登录成功跳回首页
                return "redirect:http://yunmall.com";
            }else {
                return "redirect:http://auth.yunmall.com/login.html";
            }
        }else{
            return "redirect:http://auth.yunmall.com/login.html";
        }

    }


}
